/**********************************************************************************************************************
 *  FILE DESCRIPTION
 *  -------------------------------------------------------------------------------------------------------------------
 *          File:  CtApSignalDetector.c
 *        Config:  D:/Projects/Amp/03_Software/SIP_S32K14X/Application/SipAddon/S32K144_Handle_DEMO/S32K144_Start.dpa
 *     SW-C Type:  CtApSignalDetector
 *  Generated at:  Thu Apr 25 17:23:05 2024
 *
 *     Generator:  MICROSAR RTE Generator Version 4.19.0
 *                 RTE Core Version 1.19.0
 *       License:  CBD1800257
 *
 *   Description:  C-Code implementation template for SW-C <CtApSignalDetector>
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of version logging area >>                DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

/* PRQA S 0777, 0779 EOF */ /* MD_MSR_5.1_777, MD_MSR_5.1_779 */

/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of version logging area >>                  DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

#include "Rte_CtApSignalDetector.h" /* PRQA S 0857 */ /* MD_MSR_1.1_857 */


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of include and declaration area >>        DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/
#include "Dio.h"

typedef enum
{
    SIG_STATE_OFF,
    SIG_STATE_ON
} SigState_t;

typedef struct
{
    SigState_t state;
    uint16 counter;
    uint16 channel;
    uint8 on_val;
    uint16 on_time;
    uint16 off_time;
} SigMngr_t;

typedef enum
{
    SIG_TYPE_ACC,
    SIG_TYPE_MAX
} SigType_t;

static SigMngr_t SigMngr[SIG_TYPE_MAX] =
{
    {SIG_STATE_ON, 0, DioConf_DioChannel_DioChannel_ACC, 1, 10, 10},
};

/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of include and declaration area >>          DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 *
 * Used AUTOSAR Data Types
 *
 **********************************************************************************************************************
 *
 * Primitive Types:
 * ================
 * boolean: Boolean (standard type)
 *
 *********************************************************************************************************************/


#define CtApSignalDetector_START_SEC_CODE
#include "CtApSignalDetector_MemMap.h" /* PRQA S 5087 */ /* MD_MSR_19.1 */

/**********************************************************************************************************************
 *
 * Runnable Entity Name: RE_SignalDetector_Init
 *
 *---------------------------------------------------------------------------------------------------------------------
 *
 * Executed once after the RTE is started
 *
 *********************************************************************************************************************/
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of documentation area >>                  DO NOT CHANGE THIS COMMENT!
 * Symbol: RE_SignalDetector_Init_doc
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of documentation area >>                    DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

FUNC(void, CtApSignalDetector_CODE) SignalDetector_Init(void) /* PRQA S 0850 */ /* MD_MSR_19.8 */
{
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of runnable implementation >>             DO NOT CHANGE THIS COMMENT!
 * Symbol: SignalDetector_Init
 *********************************************************************************************************************/
    for (uint16 i = 0; i < SIG_TYPE_MAX; i++)
    {
        SigMngr[i].counter = 0;
    }

/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of runnable implementation >>               DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/
}

/**********************************************************************************************************************
 *
 * Runnable Entity Name: RE_SignalDetector_Mainfunction
 *
 *---------------------------------------------------------------------------------------------------------------------
 *
 * Executed if at least one of the following trigger conditions occurred:
 *   - triggered on TimingEvent every 10ms
 *
 **********************************************************************************************************************
 *
 * Output Interfaces:
 * ==================
 *   Explicit S/R API:
 *   -----------------
 *   Std_ReturnType Rte_Write_PpSignalACC_DeAccState(boolean data)
 *
 *********************************************************************************************************************/
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of documentation area >>                  DO NOT CHANGE THIS COMMENT!
 * Symbol: RE_SignalDetector_Mainfunction_doc
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of documentation area >>                    DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/

FUNC(void, CtApSignalDetector_CODE) SignalDetector_Mainfunction(void) /* PRQA S 0850 */ /* MD_MSR_19.8 */
{
/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of runnable implementation >>             DO NOT CHANGE THIS COMMENT!
 * Symbol: SignalDetector_Mainfunction
 *********************************************************************************************************************/
    uint8 state;

    for (uint16 i = 0; i < SIG_TYPE_MAX; i++)
    {
        state = Dio_ReadChannel(SigMngr[i].channel);

        switch (SigMngr[i].state)
        {
        case SIG_STATE_OFF:
            if (state == SigMngr[i].on_val)
            {
                SigMngr[i].counter++;

                if (SigMngr[i].counter >= SigMngr[i].on_time)
                {
                    SigMngr[i].state = SIG_STATE_ON;
                    SigMngr[i].counter = 0;
                    Rte_Write_PpSignalACC_DeAccState(SigMngr[i].state);
                }
            }
            else
            {
                SigMngr[i].counter = 0;
            }
            break;

        case SIG_STATE_ON:
            if (state != SigMngr[i].on_val)
            {
                SigMngr[i].counter++;

                if (SigMngr[i].counter >= SigMngr[i].off_time)
                {
                    SigMngr[i].state = SIG_STATE_OFF;
                    SigMngr[i].counter = 0;
                    Rte_Write_PpSignalACC_DeAccState(SigMngr[i].state);
                }
            }
            else
            {
                SigMngr[i].counter = 0;
            }
            break;

        default:
            break;
        }
    }

/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of runnable implementation >>               DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/
}


#define CtApSignalDetector_STOP_SEC_CODE
#include "CtApSignalDetector_MemMap.h" /* PRQA S 5087 */ /* MD_MSR_19.1 */


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of function definition area >>            DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of function definition area >>              DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << Start of removed code area >>                   DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/


/**********************************************************************************************************************
 * DO NOT CHANGE THIS COMMENT!           << End of removed code area >>                     DO NOT CHANGE THIS COMMENT!
 *********************************************************************************************************************/
